package com.hy.service.impl;

import com.hy.bean.User;
import com.hy.dao.AdminDao;
import com.hy.service.AdminService;
import com.hy.utils.DBManager;
import com.hy.utils.PageUtils;
import com.hy.utils.ResponseResult;
import com.hy.utils.ResultUtils;
import org.apache.commons.dbutils.DbUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDao();

    @Override
    public void selectLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String phone1 = request.getParameter("phone1");
        String password = request.getParameter("password");
        List<User> users = adminDao.selectLogin(phone1, password);
        ResponseResult<List<User>> result = null;
        if (users.size() > 0) {
            String userName = users.get(0).getUserName();
            Integer status = Integer.valueOf(users.get(0).getPersonnelStatus());
            HttpSession session = request.getSession();
            session.setAttribute("admin", userName);
            result = new ResponseResult<>(200, "登录成功", users, status);
        } else {
            result = new ResponseResult<>(200, "登录失败", users, 102);
        }
        ResultUtils.result(result, request, response);
    }

    @Override
    public void tz(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getRequestDispatcher("/WEB-INF/view/show.jsp").forward(request, response);
    }

    @Override
    public void selectCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        List<Map<String, Object>> mapList = adminDao.selectCard(page - 1, limit);
        Long count = adminDao.selectCardCount();
        Long total = count % limit == 0 ? count / limit : count / limit + 1;
        PageUtils<List<Map<String, Object>>> pu = new PageUtils<>(count, total, page, limit, mapList);
        ResponseResult<PageUtils<List<Map<String, Object>>>> result = new ResponseResult<>(200, "数据", pu, 101);
        ResultUtils.result(result, request, response);
    }

    @Override
    public void updateCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String status = request.getParameter("status");
        Integer userId = Integer.parseInt(request.getParameter("userId"));

        boolean b = adminDao.updateCard(id, status, userId);
        ResponseResult<Boolean> result = null;
        if (b) {
            result = new ResponseResult<>(200, "审核成功", b, 101);
        } else {
            result = new ResponseResult<>(200, "审核失败", b, 102);
        }
        ResultUtils.result(result, request, response);
    }

    @Override
    public void selectReportUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        List<Map<String, Object>> mapList = adminDao.selectReportUser(page - 1, limit);
        Long count = adminDao.selectReportUserCount();
        Long total = count % limit == 0 ? count / limit : count / limit + 1;
        PageUtils<List<Map<String, Object>>> pu = new PageUtils<>(count, total, page, limit, mapList);
        ResponseResult<PageUtils<List<Map<String, Object>>>> result = new ResponseResult<>(200, "数据", pu, 101);
        ResultUtils.result(result, request, response);
    }

    @Override
    public void selectReportCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        List<Map<String, Object>> mapList = adminDao.selectReportCard(page - 1, limit);
        Long count = adminDao.selectReportCardCount();
        Long total = count % limit == 0 ? count / limit : count / limit + 1;
        PageUtils<List<Map<String, Object>>> pu = new PageUtils<>(count, total, page, limit, mapList);
        ResponseResult<PageUtils<List<Map<String, Object>>>> result = new ResponseResult<>(200, "数据", pu, 101);
        ResultUtils.result(result, request, response);
    }

    @Override
    public void selectReportComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        List<Map<String, Object>> mapList = adminDao.selectReportComment(page - 1, limit);
        Long count = adminDao.selectReportCommentCount();
        Long total = count % limit == 0 ? count / limit : count / limit + 1;
        PageUtils<List<Map<String, Object>>> pu = new PageUtils<>(count, total, page, limit, mapList);
        ResponseResult<PageUtils<List<Map<String, Object>>>> result = new ResponseResult<>(200, "数据", pu, 101);
        ResultUtils.result(result, request, response);
    }

    @Override
    public void updateReportUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String status = request.getParameter("status");
        Integer userId = Integer.parseInt(request.getParameter("userId"));

        Connection conn = DBManager.getConn();
        boolean flag = true;
        try {
            conn.setAutoCommit(false);
            if ("1".equals(status)) {
                List<Map<String, Object>> mapList = adminDao.selectId(conn, id);
                Object userId1 = mapList.get(0).get("user_id");
                Integer uid = Integer.parseInt(userId1.toString());
                List<Map<String, Object>> mapList1 = adminDao.selectUserStatus(uid);
                String userStatus = mapList1.get(0).get("user_status").toString();
                if ("0".equals(userStatus)) {
                    flag = adminDao.updateUserStatus(conn, uid, userId, "1");
                } else if ("1".equals(userStatus)) {
                    flag = adminDao.updateUserStatus(conn, uid, userId, "2");
                } else if ("2".equals(userStatus)) {
                    return;
                }
            }
            flag = adminDao.updateReport(id, status, userId);
        } catch (Exception e) {
            flag = false;
            conn.rollback();
            e.printStackTrace();
        } finally {
            if (flag) {
                conn.commit();
            } else {
                conn.rollback();
            }
        }
        DbUtils.close(conn);

        ResponseResult<Boolean> result = null;
        if (flag) {
            result = new ResponseResult<>(200, "举报成功", flag, 101);
        } else {
            result = new ResponseResult<>(200, "举报失败", flag, 102);
        }
        ResultUtils.result(result, request, response);
    }

    @Override
    public void updateReportCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String status = request.getParameter("status");
        Integer userId = Integer.parseInt(request.getParameter("userId"));

        Connection conn = DBManager.getConn();
        boolean flag = true;
        try {
            conn.setAutoCommit(false);
            if ("1".equals(status)) {
                List<Map<String, Object>> mapList = adminDao.selectId(conn, id);
                Object cardId = mapList.get(0).get("card_id");
                Integer cid = Integer.parseInt(cardId.toString());
                List<Map<String, Object>> maps = adminDao.selectIdComment(cid);
                List<Integer> integers = new ArrayList<>();
                for (Map list : maps) {
                    integers.add((Integer) list.get("id"));
                }
                for (Integer integer : integers) {
                    flag = adminDao.updateIdComment(conn, integer, userId);
                }
                flag = adminDao.updateIdCard(conn, cid, userId);
            }
            flag = adminDao.updateReport(id, status, userId);
        } catch (Exception e) {
            flag = false;
            conn.rollback();
            e.printStackTrace();
        } finally {
            if (flag) {
                conn.commit();
            } else {
                conn.rollback();
            }
        }
        DbUtils.close(conn);

        ResponseResult<Boolean> result = null;
        if (flag) {
            result = new ResponseResult<>(200, "举报成功", flag, 101);
        } else {
            result = new ResponseResult<>(200, "举报失败", flag, 102);
        }
        ResultUtils.result(result, request, response);
    }

    @Override
    public void updateReportComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String status = request.getParameter("status");
        Integer userId = Integer.parseInt(request.getParameter("userId"));

        Connection conn = DBManager.getConn();
        boolean flag = true;
        try {
            conn.setAutoCommit(false);
            if ("1".equals(status)) {
                List<Map<String, Object>> mapList = adminDao.selectId(conn, id);
                Object commentId = mapList.get(0).get("comment_id");
                Integer cid = Integer.parseInt(commentId.toString());
                flag = adminDao.updateIdComment(conn, cid, userId);
            }
            flag = adminDao.updateReport(id, status, userId);
        } catch (Exception e) {
            flag = false;
            conn.rollback();
            e.printStackTrace();
        } finally {
            if (flag) {
                conn.commit();
            } else {
                conn.rollback();
            }
        }
        DbUtils.close(conn);

        ResponseResult<Boolean> result = null;
        if (flag) {
            result = new ResponseResult<>(200, "举报成功", flag, 101);
        } else {
            result = new ResponseResult<>(200, "举报失败", flag, 102);
        }
        ResultUtils.result(result, request, response);
    }
}
